/*The following js  functions belong to layout.vm file in reporting\src\template\common\reporting*/
function reportFormSubmit()
{
document.forms['reportForm'].submit();
}

function disableTextFields(obj1,obj2,obj3)
{
  if (obj1.value == "All")
  {
       obj2.value="";
          obj2.disabled=true;
          obj2.style.backgroundColor = "#ccc";

       obj3.value="";
          obj3.disabled=true;
          obj3.style.backgroundColor = "#ccc";
   }
   else if (obj1.value == "Between")
   {
    obj2.style.backgroundColor = "white";
    obj3.style.backgroundColor = "white";
       obj2.disabled=false;
       obj3.disabled=false;
   }
   else
   {
     obj2.disabled=false;
        obj3.value="";
        obj3.disabled=true;
        obj2.style.backgroundColor = "white";
        obj3.style.backgroundColor = "#ccc";
   }
}


function disableDropDownBox(obj1,obj2,obj3,obj4)
{
	if (obj1.value == -999)
	{
		obj2.value="All";
		obj2.disabled=true;
		obj2.style.backgroundColor = "#eeeeee";
		disableTextFields(obj2,obj3,obj4)
	}
	else
	{
		obj2.value="All";
		obj2.disabled=false;
		obj2.style.backgroundColor = "white";
		disableTextFields(obj2,obj3,obj4)
	}
}


   /**
    * Creates a new XMLHttpRequest object.
    */
   function createHttpRequest()
   {
     var xmlhttp = null;
     if (window.ActiveXObject)
     {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     else if (window.XMLHttpRequest)
     {
       xmlhttp = new XMLHttpRequest();
     }
     return xmlhttp;
   }

   function getFormValues(fobj,valFunc)
   {
       var str = "";
       var valueArr = null;
       var val = "";
       var cmd = "";
       for(var i = 0;i < fobj.elements.length;i++)
       {
           switch(fobj.elements[i].type)
           {
               case "text":
                   if(valFunc)
                   {
                       //use single quotes for argument so that the value of
                       //fobj.elements[i].value is treated as a string not a literal
                       cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
                       val = eval(cmd)
                   }
                   str += fobj.elements[i].name +
                   "=" + escape(fobj.elements[i].value) + "&";
               break;
               case "select-one":
                   str += fobj.elements[i].name +
                   "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
               break;
               case "hidden":
                   str += fobj.elements[i].name +
                   "=" + escape(fobj.elements[i].value) + "&";
               break;
           }
       }
       str = str.substr(0,(str.length - 1));
       return str;
   }

function updateCascadingPrompts( asyncPromptUpdate)
{
 var xmlhttp = createHttpRequest();
 //alert("xmlhttp state:"+xmlhttp.readyState);
 var requestURL = document.forms['reportForm'].action;
 var parameters = getFormValues(document.forms['reportForm'],null);

 parameters=parameters+"&asyncPromptUpdate="+asyncPromptUpdate.name+"&transformerId=$transformerConfig.id";

 //alert("request url:"+requestURL);
 //alert("parameters:"+parameters);

 //for(prompt in cascadingPrompts)
 //{
 //  alert("promtp:"+prompt);
 //}
 xmlhttp.onreadystatechange = function()
   {
     if (xmlhttp.readyState == 4)
     {
       //alert("response:"+xmlhttp.responseText);
       //transferListItems(selectId, eval(xmlhttp.responseText));
       updatePrompt(asyncPromptUpdate, eval(xmlhttp.responseText));
     }
   };

 xmlhttp.open("POST", requestURL);

 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
 xmlhttp.setRequestHeader("Content-length", parameters.length);
 xmlhttp.setRequestHeader("Connection", "close");

 xmlhttp.send(parameters);

}

function updatePrompt(prompt, valueArray)
{
   if(prompt.type == "select-one")
   {
       prompt.length=0;
       //prompt.options[0] = new Option("[select]");
       for(var i = 0; i < valueArray.length; i++)
       {
           //create new option
           var option = new Option();
           option.value = valueArray[i++];
           option.text=valueArray[i];
           prompt.options[prompt.length]=option;
       }
       //alert("promt lenegth:"+prompt.length);
   }else{
     alert("Async update prompt, cannot updat this type:"+prompt.type);
   }
}








/* The following js functions belong to report_body.vm file in reporting\src\emplate\common\reporting
sets the input value in the paging forms for submitting */   
function setCellInput( element1,  element2, status){        
	element1.value=status;        
	element2.value=status;    
} 
 
   function setCellInputNew(pagingFormName1,pagingFormName2,radioKey,status){
 		if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1){
		var sucobjs=$("input[type='hidden'][name='"+radioKey+"']");
		sucobjs.attr("value",status);
		return;
		}   
		var pagingForm1=document.forms[pagingFormName1];
		var pagingForm2=document.forms[pagingFormName2];
		if(pagingForm1){
			var str1 ="document.forms['"+pagingFormName1+"']."+radioKey+".value = '"+status+"';";
			eval(str1);
		}
		if(pagingForm2){
			var str2 = "document.forms['"+pagingFormName2+"']."+radioKey+".value = '"+status+"';";
			eval(str2);
		}
 }
 function test()    {       
 //alert("HELLO before Submitting");     
 // alert($transformerConfig.getShowHeader());      
 // alert($dataSourceConfig.getDatasourceQueryConfig().size());      
 // alert($transformerConfig.getRowsConfig().getRowConfigs().size());       
 //alert($transformerConfig.getFormConfig().getAction());       
 //form = document.form_checkAll;      
 //alert(document.${reportFormName});       
 //submit();    
 }     
 
 function getLink(url) { 
 var selectedValue = document.getElementById("reportPeriodStart").value; 
 var nextValue = selectedValue - getDifference(); 
 location.href = url+nextValue;    }    
 
 
 
 function getDifference() { 
 var selectValue = document.getElementById("reportPeriodStart").value; 
 var temp=selectValue; 
 for(i=0;i<document.getElementById("reportPeriodStart").length;i++) 
 { 	    
	temp = document.getElementById("reportPeriodStart").options[i].value; 	    
	if(selectValue==temp){ 	   
		if(i < document.getElementById("reportPeriodStart").length-1){
			temp = document.getElementById("reportPeriodStart").options[i+1].value; 
			if(temp < selectValue){
				return selectValue-temp;
			}
			else if(i>0){
				temp = document.getElementById("reportPeriodStart").options[i-1].value; 
				if(temp < selectValue){
					return selectValue-temp;
				}
			}
		}
		break;  
	} 	   
 } 
 return 0;
 }    

 function disableAnchor(obj, disable){    	
 if(disable){    		
	obj.replaceWith("<span style='color:gray'>"+obj.text()+"</span>"); 
 }    
}     

function validatePreviousLink(){ 	
var selectValue = document.getElementById("statementCombineKey").value; 	
//alert("selectValue"+selectValue); 	
var temp=selectValue; 	
for(i=0;i<document.getElementById("statementCombineKey").length;i++) 
{ 	    
var temp1 = document.getElementById("statementCombineKey").options[i].value; 	    
//alert("temp1 "+temp1) 	    
if(i==0){ 	      
  temp=temp1; 	 
  }else{ 	    
  if(temp>temp1){ 	   
  temp=temp1 	  
  } 	   
  } 
  } 
  if(selectValue==temp){ 		
  disableAnchor($("a#href_id"),true);
  $("label#message4prebalance").remove();
  } 
  return true;   
  }
  
  
  
  
  
/*This js function belongs to the paging.vm file in reporting\src\emplate\common\reporting*/  
  
 /* function check_fetchBoundary(form)
{
var dispConfirm=false;
for(var i = 0; i < form.elements.length; i++)
{	
	if(form.elements[i].name.indexOf('cell_') == 0){	
	if (form.elements[i].value=="true")
	{
		dispConfirm=true;
		break;
	}
	}
		
}
if (dispConfirm==true)
{
	return confirm("Continuing to the selected page will result in loosing your current selections.");	
	
}
else
{
	if ($transformer.isSelectedInCurrentFetch()){
	   return confirm("Continuing to the selected page will result in loosing your current selections.");	
    }	
    return true;
}
}*/

/* custome_report.vm*/
/*This js function has been removed form custome_report.vm*/  
/*function checkReportName(form)
{
	if(form.reportName.value == '' ){
		alert(emptyCustomReportName); 
		return false;
	}else if(form.reportName.value.search(specialCharactersList) >= 0){
		alert(specialCharactersMsg);
		return false;
	}
	return true;
}
*/

function changeBudgetFrequency()
{
	document.forms['reportForm'].budgetPeriod.value='';
	document.forms['reportForm'].promptAction.value='changeFrequency';
	if(document.forms['reportForm'].batchAction!=null && document.forms['reportForm'].batchAction.value.equals('createReport'))
	{
		document.forms['reportForm'].tab.value='batch';
	}
	reportFormSubmit();
	return true;
}